package cn.itcast.com.homework.day86.Client1;


import java.io.*;
import java.net.Socket;
import java.util.Properties;

public class Client1 {
    public static void main(String[] args) throws IOException {
        //创建一个socket对象,声明目的服务端的ip和端口号
        Socket socket = new Socket("127.0.0.1", 8282);

        //创建Properties对象,读取配置文件，读取文件地址
        Properties properties = new Properties();
        properties.load(new InputStreamReader(new FileInputStream("D:\\代码\\代码\\代码\\cn\\itcast\\com\\homework\\day86\\configuration.properties"),"GBK"));
        String srcAddress = properties.getProperty("ImgSrcAddress");

        //创建缓冲输入流,将文件通过字节流读入
        FileInputStream fis = new FileInputStream(srcAddress);
        BufferedInputStream bis = new BufferedInputStream(fis);

        //通过socket创建缓冲输出流里，将缓冲输入流读入的东西写入输出流
        OutputStream out = socket.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);

        //默认套路
        int len;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1){
            bos.write(bytes, 0, len);
        }

        //假如直接释放资源，缓冲流中可能还有剩余的数据
        bos.flush();

        //由于read是个阻塞方法，客户端和服务端会“阻塞”,
        //通过单方面禁止客户端的输出流，解决
        socket.shutdownOutput();

        //读取服务端发来的消息
        InputStream in = socket.getInputStream();
        len = in.read(bytes);
        System.out.println(new java.lang.String(bytes, 0, len));
    }
}
